/**
 * 表单自定义控制器
 */
app.controller('btnCtrl', ['$scope', '$http', BtnController]);
function BtnController($scope, $http) {
	$scope.bindBtn = function(event) {
		switch(event){
			case 'event-to-add': // 新增工资付款单
			{
				var listUkey = [];
				for(var i=0;i<$scope.subGrid.sonlist.length;i++){
					var data = $scope.subGrid.sonlist[i];
					listUkey.push(data.userMonthPerformanceId);
				}
				$scope.showModalCheckBox('TB_UserMonthPerformanceYb', 'id', listUkey);
				break;
			}
			case 'event-to-delete': // 删除选中数据
			{
				$scope.subGrid.deleteBatch();
				//计算逻辑
				var healthCompany = 0;
				var healthPersonal = 0;
				var birthCompany = 0;
				var orgName = "";
				for (let n = 0; n < $scope.subGrid.sonlist.length; n++) {
					orgName = $scope.subGrid.sonlist[n].orgName
					healthCompany = healthCompany + Number($scope.subGrid.sonlist[n].healthCompany)
					healthPersonal = healthPersonal + Number($scope.subGrid.sonlist[n].healthPersonal)
					birthCompany = birthCompany + Number($scope.subGrid.sonlist[n].birthCompany)
				}
				$scope.valueMap['orgName'] = orgName;
				$scope.valueMap['healthCompany'] = healthCompany;
				$scope.valueMap['healthPersonal'] = healthPersonal;
				$scope.valueMap['healthTol'] = healthCompany+healthPersonal;
				break;
			}
		};
	}

}

/*
* 子列表控制器
*/
app.controller('subGridCtrl', ['$scope', '$http', function SubGridController($scope, $http){
	/*
	 * 复选框回调
	 */
	$scope.checkbox.afterSelectDatas = function(rowsSelected) {
		if($scope.subGrid.sonlist != 0){
			let newall=[]
			let endData = [];
			var isdata=false
			for (let i = 0; i < rowsSelected.length; i++) {
				var isdata=false
				if (newall.indexOf(rowsSelected[i].userMonthPerformanceId) === -1) {
					for (let n = 0; n < $scope.subGrid.sonlist.length; n++) {
						if (newall.indexOf(rowsSelected[i].userMonthPerformanceId) === -1) {
							if(rowsSelected[i].userMonthPerformanceId==$scope.subGrid.sonlist[n].userMonthPerformanceId){
								isdata=true
								endData.push($scope.subGrid.sonlist[n]);
								newall.push($scope.subGrid.sonlist[n].userMonthPerformanceId);
							}
						}
					}
					if(!isdata){
						endData.push(rowsSelected[i]);
						newall.push(rowsSelected[i].userMonthPerformanceId);
					}
				}
			}
			$scope.subGrid.sonlist = endData
		}else{
			$scope.subGrid.sonlist = rowsSelected
		}
		//计算逻辑
		var healthCompany = 0;
		var healthPersonal = 0;
		var birthCompany = 0;
		var orgName = "";
		for (let n = 0; n < $scope.subGrid.sonlist.length; n++) {
			orgName = $scope.subGrid.sonlist[n].orgName
			healthCompany = healthCompany + Number($scope.subGrid.sonlist[n].healthCompany)
			healthPersonal = healthPersonal + Number($scope.subGrid.sonlist[n].healthPersonal)
			birthCompany = birthCompany + Number($scope.subGrid.sonlist[n].birthCompany)
		}
		if ($scope.subGrid.sonlist.length != 0){
			$scope.valueMap['orgName'] = orgName;
			$scope.valueMap['healthCompany'] = healthCompany;
			$scope.valueMap['healthPersonal'] = healthPersonal;
			$scope.valueMap['healthTol'] = healthCompany+healthPersonal;
		}
	}
}]);



